#include "socket_connection.h"

#include <unistd.h>
#include <cstring>

#include "socket_data.h"

SocketConnection::SocketConnection(int connectionFd)
    : connectionFd_(connectionFd)
{
}

SocketConnection::~SocketConnection()
{
    close(connectionFd_);
}

int SocketConnection::Send(const std::string &message)
{
    struct SocketData data = {.size = message.size()};
    strncpy(data.message, message.data(), message.size());
    return write(connectionFd_, &data, data.length());
}

int SocketConnection::Recv(std::string &message)
{
    struct SocketData data = {};
    auto ret = read(connectionFd_, &data, sizeof(data));
    message = data.message;
    return ret;
}
